package view.tool;

import controller.ChangePieceListener;
import controller.SavePieceListener;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import util.Images;

/**
 * Tool that provides options which are:
 * Creating a New Piece, Saving the Current Piece, Loading a Saved Piece.
 */
public class OptionsTool extends Tool {

	private JButton newButton;
	private JButton saveButton;
	private JButton	loadButton;
	private JButton convertButton;

	public OptionsTool() {
		this.setLayout(new GridLayout(1,0,8,8));
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		this.setOpaque(false);

		newButton = createOptionButton(
			"New", Images.getNewIcon(), ChangePieceListener.NEW
		);

		saveButton = createOptionButton(
			"Save", Images.getSaveIcon(), SavePieceListener.SAVE
		);

		loadButton = createOptionButton(
			"Load", Images.getLoadIcon(), ChangePieceListener.LOAD
		);

		convertButton = createOptionButton(
			"Convert", Images.getConvertIcon(), SavePieceListener.CONVERT
		);

		this.add(newButton);
		this.add(saveButton);
		this.add(loadButton);
	}

	/**
	 * As all the buttons are similar but require a large list of method calls
	 * to format them, this method is used to create them instead.
	 */
	private JButton createOptionButton(String text, Image image, String command) {
		JButton button = new JButton(text, new ImageIcon(image));
		button.setHorizontalTextPosition(SwingConstants.CENTER);
		button.setVerticalTextPosition(SwingConstants.BOTTOM);
		button.setIconTextGap(0);
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setForeground(Color.WHITE);
		button.setMargin(new Insets(1,1,1,1));
		button.setActionCommand(command);
		return button;
	}

	public void setChangePieceListener(ChangePieceListener cpl) {
		newButton.addActionListener(cpl);
		loadButton.addActionListener(cpl);
	}

	public void setSavePieceListener(SavePieceListener spl) {
		saveButton.addActionListener(spl);
		convertButton.addActionListener(spl);
	}
}
